﻿
# 项目文件路径（可修改）
$project = "MinecraftEnchantCalculator.csproj"

# 输出目录
$output = "bin/publish"

# 目标运行时
$rids = @(
    @{rid="win-x64";  folder="MinecraftEnchantCalculator-win-x64";  ext="zip"},
    @{rid="linux-x64"; folder="MinecraftEnchantCalculator-linux-x64"; ext="tar.gz"},
    @{rid="osx-x64";   folder="MinecraftEnchantCalculator-osx-x64";  ext="zip"}
)

foreach ($target in $rids) {
    $rid = $target.rid
    $folder = $target.folder
    $ext = $target.ext
    $publishPath = "$output/$folder"

    Write-Host "正在发布 $rid ..." -ForegroundColor Cyan

    dotnet publish $project `
        -c Release `
        -r $rid `
        --self-contained false `
        -p:PublishSingleFile=true `
        -o $publishPath

    if ($LASTEXITCODE -eq 0) {
        Write-Host "发布成功: $publishPath" -ForegroundColor Green
        
        # Linux 打包时额外添加 dpkg 脚本
            if ($rid -eq "linux-x64") {
                Copy-Item "./linux-x64-dpkg.sh" -Destination $publishPath
            }

        # 压缩不同平台
        $outFile = "$output/$folder.$ext"
        if (Test-Path $outFile) {
            Remove-Item $outFile -Force
        }

        if ($ext -eq "zip") {
            Compress-Archive -Path $publishPath -DestinationPath $outFile -Force
            Write-Host "已生成压缩包: $outFile" -ForegroundColor Yellow
        }
        elseif ($ext -eq "tar.gz") {
            tar -czf $outFile -C $output $folder
            Write-Host "已生成压缩包: $outFile" -ForegroundColor Yellow
        }
    } else {
        Write-Host "发布失败: $rid" -ForegroundColor Red
        exit 1
    }
}

Write-Host "所有平台构建完成并打包！" -ForegroundColor Yellow
